import { pickedFaceData, pickedLineData, pickedVertexData } from '@amcax/base';
import { createContext, ReactNode, useContext, useState } from 'react';

export interface PlaneSharedState {
  sharedPlane: pickedFaceData | null;
  setSharedPlane: (plane: pickedFaceData | null) => void;

  // 曲面
  sharedSurface: pickedFaceData | null;
  setSharedSurface: (surface: pickedFaceData | null) => void;

  sharedPoint: pickedVertexData | null;
  setSharedPoint: (point: pickedVertexData | null) => void;

  // 仅直线
  sharedLine: pickedLineData | null;
  setSharedLine: (line: pickedLineData | null) => void;

  // 仅曲线（不包含直线）
  sharedCurve: pickedLineData | null;
  setSharedCurve: (line: pickedLineData | null) => void;
}

const PlaneContext = createContext<PlaneSharedState>({
  sharedPlane: null,
  setSharedPlane: () => {},

  sharedSurface: null,
  setSharedSurface: () => {},

  sharedPoint: null,
  setSharedPoint: () => {},

  sharedLine: null,
  setSharedLine: () => {},

  sharedCurve: null,
  setSharedCurve: () => {},
});

export const PlaneProvider = ({ children }: { children: ReactNode }) => {
  const [sharedPlane, setSharedPlane] = useState<pickedFaceData | null>(null);
  const [sharedSurface, setSharedSurface] = useState<pickedFaceData | null>(
    null,
  );
  const [sharedPoint, setSharedPoint] = useState<pickedVertexData | null>(null);
  const [sharedLine, setSharedLine] = useState<pickedLineData | null>(null);
  const [sharedCurve, setSharedCurve] = useState<pickedLineData | null>(null);

  return (
    <PlaneContext.Provider
      value={{
        sharedPlane,
        sharedSurface,
        sharedPoint,
        sharedLine,
        sharedCurve,
        setSharedPlane,
        setSharedSurface,
        setSharedPoint,
        setSharedLine,
        setSharedCurve,
      }}
    >
      {children}
    </PlaneContext.Provider>
  );
};

export const usePlaneContext = () => useContext(PlaneContext);
